package com.vueblog.webapi.controller.admin;


import com.vueblog.webapi.controller.BaseController;
import com.vueblog.webapi.exception.BusinessException;
import com.vueblog.webapi.request.CreateOrUpdateBlogRequest;
import com.vueblog.webapi.request.UpdateUserInfoRequest;
import com.vueblog.webapi.response.BaseResponse;
import com.vueblog.webapi.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author luosj
 * @since 2021-05-16
 */
@Controller
@CrossOrigin
@RequestMapping("/admin/user")
public class UserController extends BaseController {

    @Autowired
    private IUserService userService;

    /**
     * 获取当前用户信息
     */
    @GetMapping("/info")
    @ResponseBody
    public BaseResponse getCurrentUser() {
        return userService.getCurrentUser();
    }

    /**
     * 修改当前用户信息
     */
    @PutMapping("/update/{id}")
    @ResponseBody
    public BaseResponse updateUserInfo(@RequestBody @Valid UpdateUserInfoRequest request, BindingResult bindingResult, @PathVariable("id") String id) {
        checkBindingResult(bindingResult);
        try {
            return userService.updateUserInfo(request, id);
        } catch (BusinessException e) {
            return new BaseResponse(-1, e.getMsg());
        }
    }
}
